package org.ehais;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

import javazoom.jl.player.Player;
import javazoom.jl.player.advanced.AdvancedPlayer;

public class JPlayMp3Application {
	public static void play(String filePath) {
        try {
            // 获取音频输入流
            File soundFile = new File(filePath);
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
 
            // 获取音频格式
            AudioFormat format = audioIn.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
 
            // 获取Clip对象
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(audioIn);
 
            // 播放音频
            clip.start();
 
            // 等待音频播放完成（可选）
            while (!clip.isRunning()) {
                Thread.sleep(100); // 防止忙等待，稍微等待一下
            }
            while (clip.isRunning()) {
                Thread.sleep(100); // 同样，等待直到音频播放完毕
            }
 
            // 关闭Clip和音频输入流
            clip.close();
            audioIn.close();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // 恢复中断状态
        }
    }
	
	public static void jplay(String filePath) {
        try {
            InputStream inputStream = new FileInputStream(filePath);
            Player player = new Player(inputStream);
//            new Thread(player).start(); // 使用新线程播放，避免阻塞主线程
            player.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	public static void jplay(File file) {
        try {
            InputStream inputStream = new FileInputStream(file);
            Player player = new Player(inputStream);
//            new Thread(player).start(); // 使用新线程播放，避免阻塞主线程
            player.play();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	
	/**JLayer提供的AdvancedPlayer类是最灵活、功能最全的播放器类。
	 * 该类提供了很多方法**/
	public static void Mp3AdvancedPlayer(String path) {
		try {
			FileInputStream fIn = new FileInputStream((path));
			BufferedInputStream bis = new BufferedInputStream(fIn);
			AdvancedPlayer player = new AdvancedPlayer(bis);
			player.play(); //播放音频文件
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
    public static void main(String[] args) {
//        String mp3FilePath = "C:\\Users\\lgj628\\Music\\123岁-卢冠廷,林子祥.mp3"; // 替换为你的MP3文件路径
//        jplay(mp3FilePath);
    	
    	File folder = new File("C:\\Users\\lgj628\\Music\\");
    	File[] listOfFiles = folder.listFiles();
    	List<File> list = new ArrayList<File>();
    	for(File file : listOfFiles) {
    		if(file.isFile() && file.getName().indexOf(".mp3") == file.getName().length() - 4) {
    			list.add(file);
//    			System.out.println(file.getName() + "是mp3文件" + file.getPath());
//    			jplay(file);
    		}    		
    	}
    	Collections.shuffle(list);
    	int index = 0;
    	for(File file : list) {
    		System.out.println(index+" : "+file.getName() + " -- " + file.getPath());
    		jplay(file);
    		index ++;
    	}
    }
}
